package posecco.shared;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import posecco.client.TextCollection;

import com.gwtent.reflection.client.ClassType;
import com.gwtent.reflection.client.TypeOracle;
import com.google.gwt.core.client.GWT;

public class Util {

	private static String secretKey = "p053((0";
	private static TextCollection textCollection;
	
	private static void setTextCollection() {
		 
		textCollection = (TextCollection) GWT.create(TextCollection.class);
	}
	
	public static String getHash(String string) {
		
		String hashword = null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update((secretKey + string).getBytes());
			BigInteger hash = new BigInteger(1, md5.digest());
			hashword = hash.toString(16);
		} catch (NoSuchAlgorithmException nsae) {
			// ignore
		}
		return hashword;
	}

	@SuppressWarnings("rawtypes")
	public static String getMessage(String label) {
		
		if (textCollection == null) {
			
			setTextCollection();
		}
		try {
			
			ClassType classType = TypeOracle.Instance.getClassType(TextCollection.class);
			return classType.invoke(textCollection, label, (Object[])null).toString();
		}
		catch (Exception e) {
			
			e.printStackTrace();
			return null;
		}
	}
}
